package com.easybuy.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easybuy.sys.model.SysAddress;
import com.easybuy.sys.mapper.SysAddressMapper;
import com.easybuy.sys.model.SysUser;
import com.easybuy.sys.service.SysAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author SR.Y
 * @since 2020-12-05
 */
@Service
public class SysAddressServiceImpl extends ServiceImpl<SysAddressMapper, SysAddress> implements SysAddressService {

    /**
     * 添加地址
     *
     * @param address
     * @return
     */
    @Override
    public boolean creat(SysAddress address) {
        return save(address);
    }

    /**
     * 修改地址
     *
     * @param address
     * @return
     */
    @Override
    public boolean update(SysAddress address) {
        return update(address);
    }


    /**
     * 删除地址
     *
     * @param idList
     * @return
     */
    @Override
    public boolean delete(List<Integer> idList) {
        return removeByIds(idList);
    }

    /**
     * 查询全部
     *
     * @param address
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public Page<SysAddress> list(SysAddress address, Integer pageSize, Integer pageNum) {
        Integer uid = address.getUid();
        String province = address.getProvince();
        String city = address.getCity();
        String county = address.getCounty();
        Page<SysAddress> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysAddress> wrapper = new QueryWrapper<SysAddress>();
        LambdaQueryWrapper<SysAddress> lambda = wrapper.lambda();
        if (StrUtil.isNotEmpty(city)) {
            lambda.eq(SysAddress::getCity, city);
        }
        if (StrUtil.isNotEmpty(county)) {
            lambda.eq(SysAddress::getCounty, county);
        }
        if (StrUtil.isNotEmpty(province)) {
            lambda.eq(SysAddress::getProvince, province);
        }
        if (uid != null) {
            lambda.eq(SysAddress::getCity,uid);
        }
        return page(page, wrapper);
    }

    /**
     * 查询单个地址
     *
     * @param id
     * @return
     */
    @Override
    public SysAddress getOneById(Integer id) {
        return getById(id);
    }
}
